﻿using Dapper;
using Entity;
using IRepository;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Repository
{
    public class PublicAttachmentsRepository : IPublicAttachmentsRepository
    {
        private readonly IDbConnection _dbConnection;

        public PublicAttachmentsRepository(IDbConnection dbConnection)
        {
            _dbConnection = dbConnection;
        }
        /// <summary>
        /// 实现新增附件记录
        /// </summary>
        /// <param name="attachment"></param>
        /// <returns></returns>
        public async Task AddAsync(SysOrgPublicAttachments attachment)
        {
            attachment.create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");  

            // SQL插入语句（根据实际表结构调整）
            string sql = @"
                INSERT INTO sys_org_public_attachments (
                    annex_code, type_of_business, order_no, upload_image,
                    uploader, create_by, create_time
                )
                VALUES (
                    @annex_code, @type_of_business, @order_no, @upload_image,
                    @uploader, @create_by, @create_time
                )";

            await _dbConnection.ExecuteAsync(sql, attachment);
        }

        public async Task<List<SysOrgPublicAttachments>> GetByAnnexCodeAsync(string annexCode)
        {
            string sql = "SELECT * FROM sys_org_public_attachments WHERE annex_code = @AnnexCode";

            //执行sql语句并返回结果
            return (await _dbConnection.QueryAsync<SysOrgPublicAttachments>(
            sql,
            new { AnnexCode = annexCode }
        )).AsList();
        }
    }
}
